﻿using MalusAdmin.Servers;
using Microsoft.AspNetCore.Authorization;

namespace MalusAdmin.WebApi.Controllers.BackStageManger;

/// <summary>
/// 登录服务
/// </summary>
[ApiExplorerSettings(GroupName = "后台管理")]
public class SysLoginController : ApiControllerBase
{
    private readonly SysUserService _sysUserService;

    public SysLoginController(SysUserService userService)
    {
        _sysUserService = userService;
    }

    /// <summary>
    /// 用户登录
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [AllowAnonymous]
    public async Task<dynamic> Login(SysUserLoginIn input)
    {
        return await _sysUserService.Login(input);
    }


    /// <summary>
    /// 获取登录用户的信息
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [ResponseCache(Duration = 60 * 60)]
    public async Task<dynamic> GetUserInfo()
    {
        return await _sysUserService.GetUserInfo();
    }


    /// <summary>
    /// 获取登录用户的路由
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [ResponseCache(Duration = 60 * 60)]
    public async Task<dynamic> GetUserMenu()
    {
        return await _sysUserService.GetUserMenu();
    }

    private readonly string _storagePath = Path.Combine(Directory.GetCurrentDirectory(), "Uploads");
    private const string UploadsFolder = "Uploads";

    [HttpPost]
    public async Task<IActionResult> UploadFile(IFormFile file)
    {
        




        if (file == null || file.Length == 0)
            return BadRequest("No file uploaded.");

        // 确保上传文件夹存在
        var uploadPath = Path.Combine(Directory.GetCurrentDirectory(), UploadsFolder);
        Directory.CreateDirectory(uploadPath);

        // 构造完整的文件路径
        var filePath = Path.Combine(uploadPath, file.FileName);

        // 使用文件流保存文件
        using (var stream = new FileStream(filePath, FileMode.Create))
        {
            await file.CopyToAsync(stream);
        }

        return Ok(new { FilePath = filePath, FileName = file.FileName });



    }
}









